<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Pro;
use App\Att;
use App\Bid;
use DB;
use Auth;

class ProController extends Controller
{
    //借款页面
    public function jie(){
    	return view('woyaojiekuan');
    }
    public function jiePost(Request $req){
    	$pro = new Pro();
    	$att = new Att();
    	//获取用户登陆信息
    	$user = $req->user();

    	//借款表写信息
    	$pro->money = $req->money * 100;
    	$pro->mobile = $req->mobile;
    	$pro->pubtime = time();
    	$pro->uid = $user->uid;
    	$pro->name = $user->name;
    	$pro->save();

    	//附属表
    	$att->age = $req->age;
    	$att->pid = $pro->pid;
    	$att->uid = $user->uid;
    	$att->save();

    	return 'ok';
    }
    //投标页展示方法
    public function pro($pid){
        $pro = Pro::find($pid);
        return view('lijitouzi',['pro'=>$pro]);
    }

    public function touzi(Request $req,$pid){
        //获取用户登陆信息
        $user = $req->user();
        $pro  = Pro::find($pid);
        $bid  = new Bid(); 
        if ($req->money > ($pro->money-$pro->receive)/100){
            return '金额不能大于';
        }
        //写入投资信息

        $bid->uid = $user->uid;
        $bid->pid = $pro->pid;
        $bid->title = $pro->title;
        $bid->money = $req->money * 100;
        $bid->pubtime = time();
        $bid->save();

        $pro->receive += $bid->money;
        $pro->save();

        if($pro->money === $pro->receive) {
            $this->touziDone($pid);
        }
        return '购买成功';
    }

    public function touziDone($pid){
        //修改项目状态
        $pro = Pro::find($pid);
        $pro->status = 2;
        $pro->save();

        //为借款人生成还款账单,本金+利息,每月
        $amount = $pro->money / $pro->hrange + ($pro->money * $pro->rate /1200);
        $row = ['uid'=>$pro->uid,'pid'=>$pro->pid,'title'=>$pro->title];
        $row['amount'] = $amount;
        $row['status'] = 0;

        //循环显示还款账单
        $today = date('Y-m-d',time());
        for($i=1;$i<=$pro->hrange;$i++){
            $row['paydate']=date('Y-m-d',strtotime("+ $i months"));
            DB::table('hks')->insert($row);
        }

        //为投资人生成预收益账单
        $bid = Bid::where('pid',$pro->pid)->get();
        $row = [];
        $row['pid'] = $pro->pid;
        $row['title'] = $pro->title;
        $row['enddate'] = date('Y-m-d',strtotime("+ $pro->hrange months"));

        //循环投资人信息
        foreach($bid as $b){
            $row['uid'] = $b->uid;
            $row['amount'] = $b->money * $pro->rate / 36500;
            DB::table('tasks')->insert($row);
        }
    }

    //还款账单的方法
    public function myzd(){
        //获取用户登陆信息
        $user = Auth::user();
        $hks = DB::table('hks')->where('uid',$user->uid)->paginate(3);
        return view('myzd',['hks'=>$hks]);

    }

    public function mytz(){
        $user = Auth::user();
        $bid = Bid::where('bids.uid',$user->uid)->whereIn('status',[1,2])->
        join('projects','bids.pid','=','projects.pid')->get(['bids.*','projects.status']);
        return view('mytz',['bids'=>$bid]);
    }

    public function mysy(){
        $user = Auth::user();
        $grow = DB::table('grows')->where('uid',$user->uid)->orderBy('gid','desc')->get();
        return view('mysy',['grow'=>$grow]);
    }
}
   